/*
 * @: 
 * @version: 1.0
 * @Author: ziwei
 * @Date: 2021-10-07 10:52:59
 * @LastEditors: ziwei
 * @LastEditTime: 2021-10-18 17:19:19
 */
//创建仓库 处理器  preloadedState：默认状态
function createStore(reducer, preloadedState) {
    let state = preloadedState;
    let listeners = [];

    function getState() {
        return state
    }

    function dispatch(action) {
        //根据老状态和action动作，计算新状态呢
        state = reducer(state, action)
        listeners.forEach(l => l())
    }

    function subscribe(listeners) {
        listeners.push(listeners);
    }

    return {
        getState, //用来获取当前的仓库状态
        dispatch, //向仓库派发动作
        subscribe //用来订阅仓库中状态的变化
    }
}


export default createStore